package com.authority.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.authority.dao.AuthRoleTypeDAO;
import com.authority.dao.AuthUserDAO;
import com.authority.entry.AuthRoleTypeDO;
import com.authority.query.AuthRoleTypeQuery;
import com.authority.result.PageResult;
import com.authority.service.AuthRoleTypeService;
import com.authority.utils.CommonUtils;
import com.authority.utils.CommonUtils.CallBack;
import com.authority.vo.AuthRoleTypeVO;
import com.authority.vo.IdNameVO;
import com.google.common.collect.Lists;

@Service("authRoleTypeService")
@Transactional(rollbackFor = Exception.class)
public class AuthRoleTypeServiceImpl implements AuthRoleTypeService {

	@Resource
	AuthRoleTypeDAO authRoleTypeDAO;

	@Resource
	AuthUserDAO userDAO;

	@Override
	public Long saveRoleType(AuthRoleTypeDO db) throws Exception {

		return authRoleTypeDAO.insertAuthRoleType(db);
	}

	@Override
	public int removeAuthRoleTypeByIds(String ids) {

		return authRoleTypeDAO.deleteAuthRoleTypeByIds(ids);
	}

	@Override
	public int modifyAuthRoleType(AuthRoleTypeDO db) {

		return authRoleTypeDAO.updateAuthRoleType(db);
	}

	@Override
	public PageResult<AuthRoleTypeVO> getAuthRoleTypeList(AuthRoleTypeQuery query) throws Exception {

		List<AuthRoleTypeVO> list = CommonUtils.dbToVo(authRoleTypeDAO.queryAuthRoleType(query), AuthRoleTypeVO.class,
				new CallBack<AuthRoleTypeDO, AuthRoleTypeVO>() {
					@Override
					public void execute(AuthRoleTypeDO db, AuthRoleTypeVO vo) {
						vo.setOperatorName(userDAO.queryUserNameById(db.getOperatorId())); // 操作人
					}
				});

		return PageResult.create(list, authRoleTypeDAO.queryAuthRoleTypeCount(query), query.getIndex(), query.getNum());
	}

	@Override
	public AuthRoleTypeDO getAuthRoleTypeById(Long id) {

		return authRoleTypeDAO.queryAuthRoleTypeById(id);
	}

	@Override
	public String getRoleTypeNameById(Long id) {

		return authRoleTypeDAO.queryTypeNameById(id);
	}

	@Override
	public List<IdNameVO> listRoleTypeIdName(Long sysId) {

		List<IdNameVO> list = Lists.newArrayList();
		List<AuthRoleTypeDO> dbList = authRoleTypeDAO.queryAuthRoleTypeBySysId(sysId);
		for (AuthRoleTypeDO db : dbList) {
			IdNameVO vo = new IdNameVO(db.getId(), db.getName());
			list.add(vo);
		}

		return list;
	}

}
